using System;
using LS.Saving;
using UnityEngine;
using Unity.VisualScripting;

namespace LS.VisualScripting
{
    public class GetLoadedFile : SavingUnit
    {
        
        [DoNotSerialize]
        [PortLabelHidden]
        public ValueInput GroupName { get; set; }
        
        [DoNotSerialize]
        public ValueOutput Info { get; set; }
        
        [DoNotSerialize]
        public ValueOutput Name { get; set; }
        
        [DoNotSerialize]
        public ValueOutput Directory { get; set; }
        
        [DoNotSerialize]
        public ValueOutput Extension { get; set; }
        
        [DoNotSerialize]
        public ValueOutput Timestamp { get; set; }
        
        protected override void Definition()
        {
            base.Definition();
            GroupName = ValueInput<string>(nameof(GroupName), string.Empty);
            Info = ValueOutput<IDataFile>(nameof(Info));
            Name = ValueOutput<string>(nameof(Name));
            Directory = ValueOutput<string>(nameof(Directory));
            Extension = ValueOutput<string>(nameof(Extension));
            Timestamp = ValueOutput<DateTime>(nameof(Timestamp));
            Requirement(GroupName, Input);
        }

        protected override ControlOutput In(Flow flow)
        {
            string groupName =  flow.GetValue<string>(GroupName);
            IDataFile info = Target?.GetSavingGroup(groupName)?.LoadedFile;
            flow.SetValue(Info, info);
            flow.SetValue(Name, info?.FileName);
            flow.SetValue(Directory, info?.Directory);
            flow.SetValue(Extension, info?.Extension);
            flow.SetValue(Timestamp, info?.Timestamp);
            return base.In(flow);
        }
    }
}